#!/usr/bin/perl
#modify.cgi

require './remotesync-lib.pl';
&ReadParse();
&error_setup($text{'error_mesg'});
#&header($text{'mirror_title'}, "" , "index", 0, 1,);
$action=$in{'modtype'};
$line_change=$in{'line_before_change'};
if($action eq "change"){
    $ip=$in{'line_ip'};
    $mod=$in{'line_mod'};
}
if($action eq "create"){
    $ip=$in{'ip'};
    $mod=$in{'mod'};
}
$ipmod=$ip."::".$mod;
#$localpath=$in{'localpath'};
$localpath=$in{'line_path'};
$minute=$in{'minute'};
$hour=$in{'hour'};
$day=$in{'day'};
$month=$in{'month'};

@time_check=split(/ /, $line_change);
$time_for_check=$time_check[0].$time_check[1].$time_check[2].$time_check[3];
$upd=$in{'optupd'};
if($upd eq "yes"){
    $opt1="-vau";
}else{
    $opt1="-va";
}

$del=$in{'optdel'};
if($del eq "yes"){
    $opt2="--delete --ignore-errors";
}else{
    $opt2="";
}

$cropt1=$in{'cropt1'};
$cropt2=$in{'cropt2'};

$bwlimite=$in{'bw'};
if($bwlimite =~ /^(\d+)MB\/s/){
   $speed = $1 * 1024;
   $bindwidth=" "."--bwlimit=$speed";
}else{
   $bindwidth="";
}
if($action eq "change"){
    $line=$minute." ".$hour." ".$day." ".$month." * rsync ".$ip."::".$mod." ".$localpath." ".$opt1." ".$opt2.$bindwidth."\n";
#   print "<p>$line</p>\n";
#   print "<p>$line_change</p>";
#   print "<p>$ipmod</p>";
    open(FH, "/sysvol/etc/cronfile");
    @cron_change=<FH>;
    $cron_num_change=@cron_change;
    close(FH);
    unlink("/sysvol/etc/cronfile_tmp");
    open(FHB, ">>/sysvol/etc/cronfile_tmp");
    for($i=0; $i<$cron_num_change; $i++){
         @time_tmp=split(/ /, $cron_change[$i]);
         $time_check=$time_tmp[0].$time_tmp[1].$time_tmp[2].$time_tmp[3];
         if(($cron_change[$i] =~ /$ipmod/)&&($time_check eq $time_for_check)){
             print FHB $line; 
         }else{
             print FHB $cron_change[$i];
         }
    }
    close(FHB);
    unlink("/sysvol/etc/cronfile");
    rename("/sysvol/etc/cronfile_tmp","/sysvol/etc/cronfile");
}
if($action eq "create"){

#check ip modname localpath
    $checkpath=$in{'localpath'};
    @checkip=split(/\./, $ip);
    $ipnum=@checkip;
#    print $ipnum;
    if(($ipnum != 4) || ($ip=~ /[\D]$/)){
       &error($text{'mesg_wrong_ip'});    
    }
    for($ii=0; $ii<4; $ii++){
        if(($checkip[$ii]=~ /\D/) || ($checkip[$ii] > 255) ||($checkip[$ii] eq '')){
             &error($text{'mesg_wrong_ip'});
   print $ipnum;
        }
    }

    if(($mod=~ /[^a-zA-Z0-9]/) || ($mod =~ /^\d+(.*)/) || ($mod eq "end")){ 

       &error($text{'mesg_wrong_mod'});
    }

    if(!(-e $checkpath)){
       &error($text{'mesg_wrong_path'});
    }

    if(($checkpath !~ /^\//) || ($checkpath !~ /\/$/)){
       &error($text{'mesg_wrong_path'});
    }


    $line=$minute." ".$hour." ".$day." ".$month." * rsync ".$ip."::".$mod." ".$in{'localpath'}." ".$cropt1." ".$cropt2.$bindwidth;
#  print "<p>$line</p>";
   open(FH, ">>/sysvol/etc/cronfile");
   print FH "$line\n";
   close(FH);
}

system('crontab /sysvol/etc/cronfile');
system('/etc/init.d/crond restart');

#&footer("/", $text{'index'});
&redirect("index.cgi");
